package com.mapper;

import com.pojo.Admin;
import org.apache.ibatis.annotations.*;

@Mapper
public interface AdminMapper {
    @Insert("insert into admin(username,password,create_time,update_time)"+" values(#{username},#{password},now(),now())")
    int insert(String username, String password);

    @Select("SELECT id, username, password, permissions, role, create_time AS createTime, update_time AS updateTime " +
            "FROM admin WHERE id = #{id}")
    Admin selectById(Integer id);

    @Select("SELECT id, username, permissions, role, create_time AS createTime " +
            "FROM admin WHERE username = #{username}")
    Admin selectByUsername(String username);

    @Update("<script>" +
            "UPDATE admin " +
            "<set>" +
            "   <if test='username != null'>username = #{username},</if>" +
            "   <if test='password != null'>password = #{password},</if>" +
            "   <if test='permissions != null'>permissions = #{permissions},</if>" +
            "   update_time = NOW()" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateById(Admin admin);

    @Delete("DELETE FROM admin WHERE id = #{id}")
    int deleteById(Integer id);
}